package com.example.administrator.myapplication;

import android.content.Context;
import android.graphics.Color;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;

import cn.jzvd.JZDataSource;
import cn.jzvd.JZMediaManager;
import cn.jzvd.JzvdStd;

public class MyPlayer extends JzvdStd {
    public PopupWindow speedPopWindow;
    TextView tvSpeed;
    int currentSpeedIndex = 1;

    public MyPlayer(Context context) {
        super(context);
    }

    public MyPlayer(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    public void onPrepared() {
        super.onPrepared();
    }

    @Override
    public void setUp(JZDataSource jzDataSource, int screen) {
        super.setUp(jzDataSource, screen);
        if (currentScreen == SCREEN_WINDOW_FULLSCREEN) {
            tvSpeed.setVisibility(View.VISIBLE);
        } else {
            tvSpeed.setVisibility(View.GONE);
        }
    }
    @Override
    public void init(Context context) {
        super.init(context);
        tvSpeed = findViewById(R.id.tv_speed);
        tvSpeed.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        if (v.getId() == R.id.tv_speed) {
            showSpeedWindow();
        }
    }

    private void showSpeedWindow() {
        LayoutInflater inflater = (LayoutInflater) getContext()
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        final LinearLayout layout = (LinearLayout) inflater.inflate(R.layout.jz_layout_speed, null);
        OnClickListener mQualityListener = new OnClickListener() {
            public void onClick(View v) {
                currentSpeedIndex = (int) v.getTag();
                String curSpeed = getSpeedFromIndex(currentSpeedIndex)+ "X";
                tvSpeed.setText(curSpeed);
                JZMediaManager.setSpeed(getSpeedFromIndex(currentSpeedIndex));
                for (int j = 0; j < layout.getChildCount(); j++) {//设置点击之后的颜色
                    if (j == currentSpeedIndex) {
                        ((TextView) layout.getChildAt(j)).setTextColor(Color.parseColor("#fff85959"));
                    } else {
                        ((TextView) layout.getChildAt(j)).setTextColor(Color.parseColor("#ffffff"));
                    }
                }
                if (speedPopWindow != null) {
                    speedPopWindow.dismiss();
                }
            }
        };

        for (int j = 0; j < 4; j++) {
            TextView speedItem = (TextView) View.inflate(getContext(), R.layout.jz_layout_speed_item, null);
            speedItem.setText(getSpeedFromIndex(j) + "X");
            speedItem.setTag(j);
            layout.addView(speedItem, j);
            speedItem.setOnClickListener(mQualityListener);
            if (j == currentSpeedIndex) {
                speedItem.setTextColor(Color.parseColor("#fff85959"));
            }
        }

        speedPopWindow = new PopupWindow(layout, LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, true);
        speedPopWindow.setContentView(layout);
        speedPopWindow.showAsDropDown(tvSpeed);
        layout.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED);
        int offsetX = tvSpeed.getMeasuredWidth() / 3;
        int offsetY = tvSpeed.getMeasuredHeight() / 3;
        speedPopWindow.update(tvSpeed, -offsetX, -offsetY, Math.round(layout.getMeasuredWidth() * 2), layout.getMeasuredHeight());
    }

    @Override
    public int getLayoutId() {
        return R.layout.myplayer;
    }

    private float getSpeedFromIndex(int index){
        if(index == 0){
            return (float) 0.5;
        }
        if(index == 1){
            return (float) 1.0;
        }
        if(index == 2){
            return (float) 1.5;
        }
        if(index == 3){
            return (float) 2.0;
        }
        return (float) 1.0;
    }
}
